package com.xiaolun;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Author 硕鼠
 * @Date 2020/6/13 14:54
 * @Version 1.0
 * @Description 文件说明
 */
public class SessionDemo01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/heml;character = utf-8");

        //得到session
        HttpSession session = req.getSession();

        //在session中存入数据
        session.setAttribute("name", "xiaolun");

        //获取session的id
        String id = session.getId();

        //判断session是不是新创建的
        if (session.isNew()) {
            resp.getWriter().write("你刚开始创建这个session，id为:"+id);
        }else {
            resp.getWriter().write("session已经存在了，id值为："+ id);
        }

        //注销session,创建时重新生成一个。
        session.invalidate();

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
